# __author:wanghao
# data:2022/6/7
from case.base import loader
from case.base.basecase import BaseCase
from case.pages.cartpage import CartPage
from unittest import skip
from case.base import route
import json

from case.pages.storepage import StorePage

"""
小程序购物车页面测试
"""


class CartPageTest(BaseCase):
    def __init__(self, methodName='runTest'):
        super(CartPageTest, self).__init__(methodName)
        self.CartPage = CartPage(self)

    """
    case1:测试购物车页面的跳转路径是否正确
    """

    def test_01_cart_page_path(self):
        self.logger.info("case1:测试购物车页面的跳转路径是否正确")
        # 先跳转到购物车页面
        self.CartPage.switch_tab_open_cart()
        self.CartPage.check_page_path(route.cartpage_route)

    """
    case2:页面的基本元素是否存在
    """

    def test_02_page_base_element(self):
        self.logger.info("case2:页面的基本元素是否存在")
        # 先跳转到购物车页面
        self.CartPage.switch_tab_open_cart()
        # 页面基本元素是否存在
        self.CartPage.check_cart_page_base_element()

    """
    case3:一键清空购物车
    """

    def test_03_clear_cart(self):
        self.logger.info("case3:一键清空购物车")
        self.CartPage.switch_tab_open_cart()
        self.CartPage.clear_cart()

    """
    case4:删除购物车全部商品
    """

    def test_04_delete_all_goods(self):
        self.logger.info("case4:删除购物车全部商品")
        self.CartPage.switch_tab_open_cart()
        self.CartPage.delete_all_cart()

    """
    case5:加购后删除
    """

    def test_05_delete_one_goods(self):
        self.logger.info("case5:加购后删除")
        # 进入商详页
        # StorePage.to_store_good_detail_page(self)
        self.CartPage.check_add_to_cart()
        self.CartPage.to_cart_from_goods_detail()
        self.CartPage.delete_one_cart(0)

    """
    case6:加购后购买
    """

    def test_06_buy_from_cart(self):
        self.logger.info("case6:加购后购买")
        # 进入商详页
        # StorePage.to_store_good_detail_page(self)
        self.CartPage.check_add_to_cart()
        self.CartPage.to_cart_from_goods_detail()
        self.CartPage.check_cart_shopping_flow()
        self.mini.app.navigate_back()
        self.CartPage.delete_one_cart(0)
    """
    case7:购物车内点击购物车内的第一个商品，进入到商详页面
    """

    def test_07_cart_to_goods(self):
        self.logger.info("case7:购物车内点击购物车内的第一个商品，进入到商详页面")
        self.CartPage.check_add_to_cart()
        self.CartPage.to_cart_from_goods_detail()
        elements = self.page.get_elements("view.goods-name.van-multi-ellipsis--l2")
        elements[0].click()
        result = self.page.wait_for("view.goods-info__title.van-multi-ellipsis--l2")  # 等待商详页面标题渲染完成
        # 判断当前页面路径是否是商详页面
        self.CartPage.check_page_path(route.goods_alias_route)
        if result:
            goods_alias = self.page.data['posterParams']['alias']
            self.assertTrue(goods_alias, "商详页面的接口没有返回商品alias")
            element = self.page.get_element("view.goods__header-icon.goods__back")
            element.click()
            self.CartPage.delete_one_cart(0)


if __name__ == "__main__":
    loader.run(module="case.cartpage_test", config="../config.json", generate_report=True)
